package com.example.myapplication.provider;


import com.example.myapplication.ResourceTable;
import com.example.myapplication.beans.Contacts;
import com.example.myapplication.customview.RoundImage;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class ContactsProvider extends RecycleItemProvider {

    private List<Contacts> list;   //数据源
    private AbilitySlice slice;    //context对象

    public ContactsProvider(List<Contacts> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    /**
     * 数据源大小
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * 根据索引返回item
     * @param i
     * @return
     */
    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    /**
     * 返回索引
     * @param i
     * @return
     */
    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 加载每一个item内部空组件数据
     * @param i
     * @param component
     * @param componentContainer
     * @return
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {

        //使用自定义组件
        Component cpt = component;
        if (cpt == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_contacts, null, false);
        }
        Contacts contacts = list.get(i);
        Text textName = cpt.findComponentById(ResourceTable.Id_item_name);
        RoundImage imageGenderIcon = cpt.findComponentById(ResourceTable.Id_item_gender_icon);
        textName.setText(contacts.getName());
        if (contacts.getGender() == 0){//表示男士
            //调用自己定义的API
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_boy1);
        }else if (contacts.getGender() == 1){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_boy2);
        }else if (contacts.getGender() == 2){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_dolphin);
        }else if (contacts.getGender() == 3){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_cat);
        }else if (contacts.getGender() == 4){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_girl1);
        }else if (contacts.getGender() == 5){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_girl2);
        }else if (contacts.getGender() == 6){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_girl3);
        }else if (contacts.getGender() == 7){
            imageGenderIcon.setPixelMapAndCircle(ResourceTable.Media_rabbit);
        }

        return cpt;

        //使用系统组件
//        Component cpt = component;
//        if (cpt == null){
//            //找到模板，通讯录列表
//            //加载布局文件获取Component对象
//            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_contacts,null,false);
//        }
//        Contacts contacts = list.get(i);
//        Text textName = cpt.findComponentById(ResourceTable.Id_item_name);
//        Image imageGenderIcon = cpt.findComponentById(ResourceTable.Id_item_gender_icon);
//        textName.setText(contacts.getName());
//        if (contacts.getGender() == 0){//表示男士
//            imageGenderIcon.setPixelMap(ResourceTable.Media_boy1);
//        }else if (contacts.getGender() == 1){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_boy2);
//        }else if (contacts.getGender() == 2){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_dolphin);
//        }else if (contacts.getGender() == 3){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_cat);
//        }else if (contacts.getGender() == 4){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_girl1);
//        }else if (contacts.getGender() == 5){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_girl2);
//        }else if (contacts.getGender() == 6){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_girl3);
//        }else if (contacts.getGender() == 7){
//            imageGenderIcon.setPixelMap(ResourceTable.Media_rabbit);
//        }
//        return cpt;

    }
}
